#include "process.h"
#include <string.h>
#include <unistd.h>

#define LENGTH 101
#define LABEL '#'
void Process()
{
    const char* symb = "|/-\\";
    int s_len = strlen(symb);
    char bar[LENGTH];
    memset(bar,'\0',sizeof(bar));
    int cnt = 0;
    while(cnt <=100)
    {
        printf("[%-100s][%d%%][%c]\r",bar,cnt,symb[cnt%s_len]);
        fflush(stdout);
        bar[cnt++] = LABEL;
        usleep(100000);
    }
    printf("\n");
}


void FlushProcess(double target,double current)
{
    //定义光标
    const static  char* sy = "|/-\\";
    static int index = 0;
    int len = strlen(sy);


    //更新出进度
    double rate = current/target*100;
    //整数个进度递增，刷新一次#号
    int cnt = (int)rate;
    //构建缓冲区
    char bar[LENGTH];
    memset(bar,0,sizeof(bar));
    for(int i=0;i<cnt;i++)
    {
        bar[i] = LABEL;
    }
    //刷新
    printf("[%-100s][%.2lf%%][%c]\r",bar,rate,sy[index++]);
    index = index%len;
    fflush(stdout);
}
